package com.zc.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @description:
 * @author: cong
 * @create: 2025/3/12
 */
@Configuration
public class RabbitMQConfig {
    public static final String QUEUE_NAME = "queue";
    public static final String EXCHANGE_NAME = "exchange";
    public static final String ROUTING_KEY = "routingKey";

    /**
     * 创建一个持久化的RabbitMQ队列
     *
     * @return Queue实例，具有给定的队列名称和持久化特性
     */
    @Bean
    public Queue queue() {
        return new Queue(QUEUE_NAME, true);
    }

    /**
     * 创建并配置一个Topic类型的交换机
     *
     * @return TopicExchange对象，代表一个Topic类型的交换机
     */
    @Bean
    public TopicExchange exchange() {
        return new TopicExchange(EXCHANGE_NAME);
    }

    /**
     * 创建一个绑定对象，用于将队列绑定到主题交换机
     *
     * @param queue 需要绑定的队列对象
     * @param exchange 主题交换机对象，用于发布消息到多个订阅的队列
     * @return 返回一个绑定对象，它定义了队列如何从交换机接收消息
     */
    @Bean
    public Binding binding(Queue queue, TopicExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(ROUTING_KEY);
    }
}
